
'use strict';
angular.module('app')
	.controller('indexController',[  '$scope','$state','ajaxRquest', function($scope,$state,ajaxRquest) {
		$scope.showModel=false;
		$scope.mobile=$.cookie("phone")
		var platform=$.cookie("platform");
		var webType=$.cookie("webType");
		var channel = $.cookie("channel");
		var uid = $.cookie("uid")
		/*console.log(platform)*/
		//渠道(hyz-慧驿站，lbjr-金融，lbn-老板娘，ld-铃铛，zqh-智仟汇)
		if(platform == "hyz"){
			$scope.platform ="慧驿站"
		}else if(platform =="lbjr"){
			$scope.platform="联璧金融"
		}else if(platform =="lbn"){
			$scope.platform="老板娘"
		}else if(platform =="ld"){
			$scope.platform="铃铛"
		}
		else{
			$scope.platform="智仟汇"
		}

		$scope.getVerificationCode=function(){
			//获取手机验证码接口-王坤
			ajaxRquest.getData('/userCenter/sendSmsCode',{
				mobile:$.cookie("phone"), //	String	手机号	是
				opType:"login",            //（login—登录/注册的合并）
			}).then(function(res){
				/*console.log(res);*/
				if(res.resCode === "00000"){
					$scope.watchVerifiCode();
				}else{
					$scope.formError({text:res.resMsg});
				}
			})
		}
		$scope.watchVerifiCode=function(){
			$scope.$apply(function () {
				$scope.showModel=true;
			});
			$scope.$watch('one', function (value) {
				if (value) {
					$(".modelBox").find('input').eq(1).focus()
					if ($scope.four) {
						$scope.postDate()
					}
				} else if (value == '') {
					$(".modelBox").find('input').css("border", "0rem");
					$scope.isShow = false;
				}
			})
			$scope.$watch('two', function (value) {
				if (value) {
					$(".modelBox").find('input').eq(2).focus()
					if ($scope.four) {
						$scope.postDate()
					}
				} else if (value == '') {
					$(".modelBox").find('input').css("border", "0rem");
					$scope.isShow = false;
					$(".modelBox").find('input').eq(0).focus()
				}
			})
			$scope.$watch('three', function (value) {
				if (value) {
					$(".modelBox").find('input').eq(3).focus()
					if ($scope.four) {
						$scope.postDate()
					}
				} else if (value == '') {
					$(".modelBox").find('input').eq(1).focus()
					$(".modelBox").find('input').css("border", "0rem");
					$scope.isShow = false;
				}
			})
			$scope.$watch('four', function (value) {
				if (value) {
					$scope.postDate()
				} else if (value == '') {
					$(".modelBox").find('input').css("border", "0rem");
					$scope.isShow = false;
					$(".modelBox").find('input').eq(2).focus()
				}
			})
		}


		$scope.postDate = function () {
			if (!$scope.one || !$scope.two || !$scope.three || !$scope.four) {
				//验证码未输完整
				return false
			} else {
				var verifyCode = $scope.one+$scope.two+$scope.three+$scope.four;
				var platform = $.cookie("platform");
				var uid = $.cookie("uid")
				var isRegisteredFlag= $.cookie("isRegisteredFlag")
				if(isRegisteredFlag="N"){
					//登录/注册慧驿站接口– 王坤
					
					ajaxRquest.getData('/userCenter/loginOrRegister',{
						mobile:$.cookie("phone"), //	String	手机号	是
						verifyCode:verifyCode,    //   String  验证码     是
						channel:platform,         //   String  渠道(hyz-慧驿站，lbjr-金融，lbn-老板娘，ld-铃铛，zqh-智仟汇)  是
						uid:uid,          //   String  渠道对应的uid    是
					}).then(function(res){
						if(res.resCode === "00000"){
							var huiToken=res.record.huiToken;
							$.cookie("huiToken",huiToken);
						      window.history.go(-1);
							// window.location.href = "http://ihui.lincomb.com/iFengChao-Hui-UI/#/access/activity?" +
							// "uid="+uid+"&phone="+$.cookie("phone")+"&platform="+platform+"&webType="+webType+"&channel="+channel
						}else{
							$(".modelBox").find('input').css("border", "1px red solid");
							$scope.$apply(function () {
								$scope.isShow = true;
							})

						}
					})
				}else{
					//授权绑定UC接口– 王坤
					ajaxRquest.getData('/userCenter/bind',{
						mobile:$.cookie("phone"), //	String	手机号	是
						verifyCode:verifyCode,    //   String  验证码     是
						channel:platform,         //   String  渠道(hyz-慧驿站，lbjr-金融，lbn-老板娘，ld-铃铛，zqh-智仟汇)  是
						uid:uid,          //   String  渠道对应的    uid    是
					}).then(function(res){
						/*console.log("授权绑定UC接口"+res);*/
						if(res.resCode === "00000"){
							var huiToken=res.record.huiToken;
							/*console.log("UChuiToken:"+huiToken)*/
							$.cookie("huiToken",huiToken);
							window.history.go(-1)
						}else{
							$(".modelBox").find('input').css("border", "1px red solid");
							$scope.$apply(function () {
								$scope.isShow = true;
							})

						}
					})
				}
			}
		}
		$scope.closeModel=function(){
			$scope.showModel=false;
		}
} ]);

//(function (window, document) {
//		var active = 0;
//		var getInputVal="";
//		var errorMsg=document.getElementById("errorMsg");
//		var inputBtn = document.querySelectorAll('input');
//		console.log(inputBtn.length);
//		for (var i =0 ; i < inputBtn.length; i++) {
//			inputBtn[i].addEventListener('click', function () {
//				inputBtn[active].focus();
//			}, false);
//			inputBtn[i].addEventListener('focus', function () {
//				this.addEventListener('keyup', listenKeyUp, false);
//			}, false);
//			inputBtn[i].addEventListener('blur', function () {
//				this.removeEventListener('keyup', listenKeyUp, false);
//			}, false);
//		}
//		/**
//		 * 监听键盘的敲击事件
//		 */
//		function listenKeyUp() {
//			var beginBtn = document.querySelector('#beginBtn');
//			if (!isNaN(this.value) && this.value.length !=0 ) {
//				if (active < 3) {
//					active +=1 ;
//				}else if(active == 3){
//					for (var j =0 ; j < inputBtn.length; j++) {
//						getInputVal +=inputBtn[j].value;
//					}
//					if(getInputVal == 1234){
//						alert("ok")
//					}else{
//						//errorMsg.style.display="block";
//						$scope.$apply(function () {
//							$scope.isShow=true
//						})
//
//					}
//
//				}
//				/*console.log( this.value);*/
//				inputBtn[active].focus();
//			} else if (this.value.length == 0) {
//				if (active >0 ) {
//					active -=1 ;
//					console.log("cative-:"+active)
//				}
//				console.log( this.value);
//				inputBtn[active].focus();
//			}
//		}
//	}
//
//)(window, document);